package com.sjm.suchbear.minibookmall.manage;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.BookDB;
import com.sjm.suchbear.minibookmall.BookDetailActivity;
import com.sjm.suchbear.minibookmall.EditInfoActivity;
import com.sjm.suchbear.minibookmall.OrderAdapter;
import com.sjm.suchbear.minibookmall.OrderDetailActivity;
import com.sjm.suchbear.minibookmall.R;
import com.sjm.suchbear.minibookmall.UserListAdapter;
import com.sjm.suchbear.minibookmall.Utils.T;
import com.sjm.suchbear.minibookmall.View.MyListView;
import com.sjm.suchbear.minibookmall.action.BookManage;
import com.sjm.suchbear.minibookmall.action.NoticeManage;
import com.sjm.suchbear.minibookmall.action.OrderManage;
import com.sjm.suchbear.minibookmall.action.UserManage;
import com.sjm.suchbear.minibookmall.fragment.adapter.BookListAdapter;
import com.sjm.suchbear.minibookmall.fragment.adapter.NoticeAdapter;
import com.sjm.suchbear.minibookmall.model.BookModel;
import com.sjm.suchbear.minibookmall.model.NoticeModel;
import com.sjm.suchbear.minibookmall.model.OrderModel;
import com.sjm.suchbear.minibookmall.model.UserModel;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ListActivity extends Activity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.lv_showList)
    MyListView lvShowList;

    private SQLiteDatabase dbread;
    private int mode;  //0:书籍列表 1:用户列表 2:订单列表 3:公告列表
    private List<BookModel> books;
    private List<UserModel> users;
    private List<OrderModel> orders;
    private List<NoticeModel> notices;
    private BookListAdapter bookListAdapter;
    private NoticeAdapter noticeAdapter;
    private OrderAdapter orderAdapter;
    private UserListAdapter userListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);
        ButterKnife.bind(this);
        BookDB db = new BookDB(getBaseContext());
        dbread = db.getReadableDatabase();
        mode = getIntent().getIntExtra("mode", 0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        switch (mode) {
            case 0:
                tvListTitle.setText("书籍列表");
                books = BookManage.getBookList(dbread);
                bookListAdapter = new BookListAdapter(getBaseContext(), books);
                lvShowList.setAdapter(bookListAdapter);
                lvShowList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent intent = new Intent(ListActivity.this, BookDetailActivity.class);
                        intent.putExtra("bookID", books.get(i).getBookID());
                        startActivity(intent);
                    }
                });
                break;
            case 1:
                tvListTitle.setText("用户列表");
                users = UserManage.getUserList(dbread);
                userListAdapter = new UserListAdapter(getBaseContext(), users);
                lvShowList.setAdapter(userListAdapter);
                lvShowList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent intent = new Intent(ListActivity.this, EditInfoActivity.class);
                        intent.putExtra("mode", 0);
                        intent.putExtra("userID", users.get(i).getUserID());
                        startActivity(intent);
                    }
                });
                break;
            case 2:
                tvListTitle.setText("订单列表");
                orders = OrderManage.getOrderList(dbread);
                orderAdapter = new OrderAdapter(getBaseContext(), orders);
                lvShowList.setAdapter(orderAdapter);
                lvShowList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        Intent intent = new Intent(ListActivity.this, OrderDetailActivity.class);
                        intent.putExtra("orderID", orders.get(i).getOrderID());
                        startActivity(intent);
                    }
                });
                break;
            case 3:
                tvListTitle.setText("公告列表");
                notices = NoticeManage.getNoticeList(dbread);
                noticeAdapter = new NoticeAdapter(getBaseContext(), notices);
                lvShowList.setAdapter(noticeAdapter);
                lvShowList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                    @Override
                    public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, long l) {
                        AlertDialog.Builder builder = new AlertDialog.Builder(ListActivity.this);
                        builder.setTitle("删除公告")
                                .setMessage("确认删除这条公告吗？")
                                .setCancelable(true)
                                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int j) {
                                        NoticeManage.deleteNotice(dbread, notices.get(i).getNoticeID());
                                        T.showShort(getBaseContext(), "删除成功");
                                        notices.remove(i);
                                        noticeAdapter.notifyDataSetChanged();
                                    }
                                })
                                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {

                                    }
                                });
                        AlertDialog ad = builder.create();
                        ad.show();
                        return false;
                    }
                });
                break;
        }
    }

    @OnClick(R.id.ll_header_back)
    public void onViewClicked() {
        finish();
    }
}
